'\" te
.\" Copyright (c) 2006, Sun Microsystems, Inc.  All Rights Reserved.
.\" Copyright 1989 AT&T
.\" The contents of this file are subject to the terms of the Common Development and Distribution License (the "License").  You may not use this file except in compliance with the License.
.\" You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE or http://www.opensolaris.org/os/licensing.  See the License for the specific language governing permissions and limitations under the License.
.\" When distributing Covered Code, include this CDDL HEADER in each file and include the License file at usr/src/OPENSOLARIS.LICENSE.  If applicable, add the following below this CDDL HEADER, with the fields enclosed by brackets "[]" replaced with your own identifying information: Portions Copyright [yyyy] [name of copyright owner]
.TH SEMGET 2 "Aug 14, 2006"
.SH NAME
semget \- get set of semaphores
.SH SYNOPSIS
.LP
.nf
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/sem.h>

\fBint\fR \fBsemget\fR(\fBkey_t\fR \fIkey\fR, \fBint\fR \fInsems\fR, \fBint\fR \fIsemflg\fR);
.fi

.SH DESCRIPTION
.sp
.LP
The \fBsemget()\fR function returns the semaphore identifier associated with
\fIkey\fR.
.sp
.LP
A semaphore identifier and associated data structure and set containing
\fInsems\fR semaphores (see \fBIntro\fR(2)) are created for \fIkey\fR if one of
the following is true:
.RS +4
.TP
.ie t \(bu
.el o
\fIkey\fR is equal to \fBIPC_PRIVATE\fR.
.RE
.RS +4
.TP
.ie t \(bu
.el o
\fIkey\fR does not already have a semaphore identifier associated with it, and
(\fIsemflg\fR\fB&IPC_CREAT\fR) is true.
.RE
.sp
.LP
On creation, the data structure associated with the new semaphore identifier is
initialized as follows:
.RS +4
.TP
.ie t \(bu
.el o
\fBsem_perm.cuid\fR, \fBsem_perm.uid\fR, \fBsem_perm.cgid\fR, and
\fBsem_perm.gid\fR are set equal to the effective user \fBID\fR and effective
group \fBID,\fR respectively, of the calling process.
.RE
.RS +4
.TP
.ie t \(bu
.el o
The access permission bits of \fBsem_perm.mode\fR are set equal to the access
permission bits of \fIsemflg\fR.
.RE
.RS +4
.TP
.ie t \(bu
.el o
\fBsem_nsems\fR is set equal to the value of \fInsems\fR.
.RE
.RS +4
.TP
.ie t \(bu
.el o
\fBsem_otime\fR is set equal to 0 and \fBsem_ctime\fR is set equal to the
current time.
.RE
.SH RETURN VALUES
.sp
.LP
Upon successful completion, a non-negative integer representing a semaphore
identifier is returned. Otherwise, \fB\(mi1\fR is returned and \fBerrno\fR is
set to indicate the error.
.SH ERRORS
.sp
.LP
The \fBsemget()\fR function will fail if:
.sp
.ne 2
.na
\fB\fBEACCES\fR\fR
.ad
.RS 10n
A semaphore identifier exists for \fIkey\fR, but operation permission (see
\fBIntro\fR(2)) as specified by the low-order 9 bits of \fIsemflg\fR would not
be granted.
.RE

.sp
.ne 2
.na
\fB\fBEEXIST\fR\fR
.ad
.RS 10n
A semaphore identifier exists for \fIkey\fR but both
(\fIsemflg\fR\fB&IPC_CREAT\fR) and (\fIsemflg\fR\fB&IPC_EXCL\fR) are both true.
.RE

.sp
.ne 2
.na
\fB\fBEINVAL\fR\fR
.ad
.RS 10n
The \fInsems\fR argument is either less than or equal to 0 or greater than the
system-imposed limit. See NOTES.
.sp
A semaphore identifier exists for \fIkey\fR, but the number of semaphores in
the set associated with it is less than \fInsems\fR and \fInsems\fR is not
equal to 0.
.RE

.sp
.ne 2
.na
\fB\fBENOENT\fR\fR
.ad
.RS 10n
A semaphore identifier does not exist for \fIkey\fR and
(\fIsemflg\fR\fB&IPC_CREAT\fR) is false.
.RE

.sp
.ne 2
.na
\fB\fBENOSPC\fR\fR
.ad
.RS 10n
A semaphore identifier is to be created but the system-imposed limit on the
maximum number of allowed semaphores or semaphore identifiers system-wide would
be exceeded. See NOTES.
.RE

.SH ATTRIBUTES
.sp
.LP
See \fBattributes\fR(7) for descriptions of the following attributes:
.sp

.sp
.TS
box;
c | c
l | l .
ATTRIBUTE TYPE	ATTRIBUTE VALUE
_
Interface Stability	Standard
.TE

.SH SEE ALSO
.sp
.LP
.BR ipcrm (1),
.BR ipcs (1),
.BR Intro (2),
.BR semctl (2),
.BR semop (2),
.BR setrctl (2),
.BR ftok (3C),
.BR attributes (7),
.BR standards (7),
.BR rctladm (8)
.SH NOTES
.sp
.LP
The system-imposed limit on the value of the \fInsems\fR argument is the
maintained on a per-process basis using the \fBprocess.max-sem-nsems\fR
resource control.
.sp
.LP
The system-imposed limit on the number of semaphore identifiers is maintained
on a per-project basis using the \fBproject.max-sem-ids\fR resource control.
The  \fBzone.max-sem-ids\fR resource control limis the total number of
semaphore identifiers that can be allocated by a zone.
.sp
.LP
See \fBrctladm\fR(8) and \fBsetrctl\fR(2) for information about using resource
controls.
